<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/include/page.jsp"%>
<%@ include file="/common/include/title.jsp"%>
<c:set value="${requestScope.response.data.one }" var="role" />
<jsp:useBean class="com.wangsh.usersCenter.users.pojo.ARole" id="rolePojo"/>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="${rootPath}/common/resource/H-ui.admin/lib/zTree/v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
<title>Insert title here</title>
</head>
<body>
	<article class="page-container">
		<div style="text-align:center">
			<form action="" class="form form-horizontal">
					<input id="roleId" type="hidden" value="${roleId}" />
					
				<div style="text-align:center">
					<ul class="ztree" id="ztree2" style="margin: 0 auto;"></ul>
				</div>
				<div class="row cl">
					<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
						<input class="btn btn-primary radius" id="button" type="button" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
					</div>
				</div>
			</form>
		</div>
	</article>
</body>
<script type="text/javascript" src="${rootPath}/common/resource/H-ui.admin/lib/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script> 
<%@ include file="/common/include/footer.jsp" %>
<script type="text/javascript">
	var setting = {
		check : {
			enable : true,
			chkStyle : "checkbox",
			chkboxType : {
				"Y" : "p",
				"N" : "s"
			}
		},
		data : {
			simpleData : {
				enable : true, //开启简单数据模式
				idKey : "id", //通过id属性指定节点标识
				pIdKey : "pId", //通过pId属性指定上节点ID
				rootPId : 0
			//根节点ID 是0
			}
		},
		callback : {
			beforeCheck : true,
		}
	};
	var node = [];
	"<c:forEach items ='${menus}'  var='amenu1'>"
	var nodes2 = {
		"id" : '${amenu1.id}',
		"name" : '${amenu1.name}',
		"pId" : '${amenu1.parentId}'
	};
	node.push(nodes2);
	"</c:forEach>"
	
	var tree = $.fn.zTree.init($("#ztree2"), setting, node); 
	
	//var nodes = tree.getNodes();
	var nodes = tree.transformToArray(tree.getNodes());
	
	var MenuIds = ${MenuIds};
	
	for (var i = 0; i < nodes.length; i++) {
		for(var j = 0;j < MenuIds.length;j++){
		if (nodes[i].id == MenuIds[j]) {
			//treeObj.selectNode(nodes[i]);//根据该节点选中
			tree.checkNode(nodes[i], true, true)
		  }
		}
	}
	$("#button").click(function() {
		var menuId = [];
		var treeObj = $.fn.zTree.getZTreeObj("ztree2");
		nodes = treeObj.getCheckedNodes(true);
		for (var i = 0; i < nodes.length; i++) {
			menuId.push(nodes[i].id);
		}
		var roleId = document.getElementById("roleId").value;
		var menusId = menuId + '';
		$.ajax({
			url : '${rootPath}/back/rbac/saveRoleMenu.htm',
			type : 'post',
			data : {
				'roleId' : roleId,
				"menuId" : menusId
			},
			success : function(dataMap) {
				console.log(typeof(dataMap))
				console.log(dataMap)
				if ("0" === dataMap) {
					alert("成功")
				} else {
					alert("失败")
				}
			}
		});
	});
</script>
</html>